package com.alibaba.citrus.ots.b2b.trade.order.repository.impl;

import com.alibaba.fastjson.JSONObject;
import com.epoch.app.otsb2btradecenter.domain.order.model.Order;
import com.epoch.app.otsb2btradecenter.domain.orderline.model.OrderLine;
import com.epoch.app.otsb2btradecenter.sdo.OrderLineSDO;
import com.epoch.app.otsb2btradecenter.sdo.OrderSDO;
import com.epoch.app.otsb2btradecenter.sdo.ScItemSDO;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @description: 参数转换
 * @author: qianlei
 * @create: 2021/11/08 22:28
 */
public class OrderConvert {

    public static OrderSDO convertToOrderSDO(Order order,List<OrderLine> orderLines){
        OrderSDO orderSDO = new OrderSDO();
        orderSDO.setOrderLineSDOS(convertToOrderLineSDOList(orderLines));
        orderSDO.setCustomerCode(order.getCustomerCode());
        orderSDO.setCustomerName(order.getCustomerName());
        orderSDO.setOrganizationCode(order.getOrganizationCode());
        orderSDO.setOrganizationName(order.getOrganizationName());
        orderSDO.setDepartmentCode(order.getDepartmentCode());
        orderSDO.setDepartmentName(order.getDepartmentName());
        orderSDO.setSaleChannelCode(order.getSaleChannelCode());
        orderSDO.setSaleChannelName(order.getSaleChannelName());
        orderSDO.setChannelCode(order.getChannelCode());
        orderSDO.setChannelName(order.getChannelName());
        orderSDO.setReceiveAddressCode(order.getReceiveAddressCode());
        orderSDO.setReceiveDetailAddress(order.getReceiveDetailAddress());
//        orderSDO.setDeliveryTime();
        orderSDO.setTransportModeCode(order.getTransportModeCode());
        orderSDO.setTransportModeName(order.getTransportModeName());
        orderSDO.setId(String.valueOf(order.getId()));
        orderSDO.setFrontOrderId(order.getFrontOrderId());
        orderSDO.setBizCode(order.getBizCode());
        orderSDO.setBusinessCode(order.getBusinessCode());
//        orderSDO.setTenantId();
        orderSDO.setProductGroupCode(order.getProductGroupCode());
        orderSDO.setProductGroupName(order.getProductGroupName());
        orderSDO.setStatus(order.getStatus());
//        orderSDO.setStatusDesc();
        orderSDO.setCreateReason(order.getCreateReason());
        orderSDO.setDeliveryStatus(order.getDeliveryStatus());
        orderSDO.setEndTime(order.getEndTime());
        Map<String,String> mainFeatures = new HashMap<>();
        if (StringUtils.isNotBlank(order.getFeatures())){
            mainFeatures = JSONObject.parseObject(order.getFeatures(), Map.class);
        }
        orderSDO.setFeatures(mainFeatures);
        orderSDO.setRemark(order.getRemark());
        orderSDO.setGmtCreate(order.getGmtCreate());
        orderSDO.setGmtModified(order.getGmtModified());
//        orderSDO.setCreatorId();
        orderSDO.setCreatorName(order.getCreatorName());
//        orderSDO.setModifierId();
        orderSDO.setModifierName(order.getModifierName());
        orderSDO.setSyncVersion(order.getSyncVersion());
        orderSDO.setOuterOrderId(order.getOuterOrderId());
//        orderSDO.setIdemId();
        orderSDO.setWareHouseCode(order.getWareHouseCode());
        orderSDO.setWareHouseName(order.getWareHouseName());
//        orderSDO.setCustomerReferenceNumber();
        orderSDO.setCustomerExpectDate(order.getCustomerExpectDate());
        orderSDO.setCostCenter(order.getCostCenter());
        orderSDO.setCashCustomerName(order.getCashCustomerName());
        orderSDO.setOrderType(order.getOrderType());
//        orderSDO.setOrderTypeName();
        orderSDO.setOrderTag(order.getOrderTag());
        orderSDO.setReverseReason(order.getReverseReason());
        orderSDO.setOrderBizId(order.getOrderBizId());
//        orderSDO.setContactor();
//        orderSDO.setContactorPhone();
//        orderSDO.setFeatureStr();
//        orderSDO.setPaymentInformationSDO();
//        orderSDO.setDeliverOrderSDO();
//        orderSDO.setAddressCode();
        orderSDO.setCustomerAccounts(order.getCustomerAccounts());
        return orderSDO;
    }

    public static List<OrderLineSDO> convertToOrderLineSDOList(List<OrderLine> orderLines){
        if (CollectionUtils.isEmpty(orderLines)){
            return new ArrayList<>();
        }
        return orderLines.stream().map(OrderConvert::convertToOrderLineSDO).collect(Collectors.toList());
    }

    public static OrderLineSDO convertToOrderLineSDO(OrderLine orderLine){
        OrderLineSDO orderLineSDO = new OrderLineSDO();
        orderLineSDO.setPayStatus(orderLine.getPayStatus());
        orderLineSDO.setPayTime(orderLine.getPayTime());
        orderLineSDO.setShouldPayFee(orderLine.getShouldPayFee() == null?null:Long.valueOf(orderLine.getShouldPayFee()));
        orderLineSDO.setActualPaidFee(orderLine.getActualPaidFee() == null?null:Long.valueOf(orderLine.getActualPaidFee()));
        orderLineSDO.setId(String.valueOf(orderLine.getId()));
        orderLineSDO.setMainOrderId(orderLine.getMainOrderId());
        orderLineSDO.setSerialNumber(orderLine.getSerialNumber());
        orderLineSDO.setOrderLineTag(orderLine.getOrderLineTag());
        orderLineSDO.setBizCode(orderLine.getBizCode());
        orderLineSDO.setOuterOrderLineId(orderLine.getOuterOrderLineId());
        orderLineSDO.setCustomerCode(orderLine.getCustomerCode());
        orderLineSDO.setStatus(orderLine.getStatus());
        orderLineSDO.setDeliveryStatus(orderLine.getDeliveryStatus());
        orderLineSDO.setDeliveryTime(orderLine.getDeliveryTime());
        orderLineSDO.setEndTime(orderLine.getEndTime());
        orderLineSDO.setActivityName(orderLine.getActivityName());
        orderLineSDO.setActivityCode(orderLine.getActivityCode());
        orderLineSDO.setFrontOrderLineId(orderLine.getFrontOrderLineId());
        orderLineSDO.setRemark(orderLine.getRemark());

        orderLineSDO.setRefuseReasonName(orderLine.getRefuseReasonName());
        orderLineSDO.setRefuseReasonCode(orderLine.getRefuseReasonCode());
        orderLineSDO.setDeliveryAmount(orderLine.getDeliveryAmount());
//        orderLineSDO.setTenantId();
        orderLineSDO.setGmtCreate(orderLine.getGmtCreate());
        orderLineSDO.setGmtModified(orderLine.getGmtModified());
        Map<String,String> subFeatures = new HashMap<>();
        if (StringUtils.isNotBlank(orderLine.getFeatures())){
            subFeatures = JSONObject.parseObject(orderLine.getFeatures(), Map.class);
        }
        orderLineSDO.setFeatures(subFeatures);
        orderLineSDO.setSyncVersion(orderLine.getSyncVersion());
        orderLineSDO.setCreatorId(orderLine.getCreatorId());
        orderLineSDO.setCreatorName(orderLine.getCreatorName());
        orderLineSDO.setModifierId(orderLine.getModifierId());
        orderLineSDO.setModifierName(orderLine.getModifierName());
        orderLineSDO.setReturnCharacter(orderLine.getReturnCharacter());


        ScItemSDO scItemSDO = new ScItemSDO();
        scItemSDO.setUnitConvert(orderLine.getUnitConvert());
        scItemSDO.setSaleUnit(orderLine.getSaleUnit());
        scItemSDO.setInventoryUnitName(orderLine.getInventoryUnit());
        scItemSDO.setInventoryUnitCode(orderLine.getInventoryUnitCode());
        scItemSDO.setScItemId(orderLine.getScItemId());
        scItemSDO.setScItemTitle(orderLine.getScItemTitle());
        scItemSDO.setProductGroupCode(orderLine.getProductGroupCode());
        scItemSDO.setProductGroupName(orderLine.getProductGroupName());
        scItemSDO.setUnitChangeRelation(orderLine.getUnitConvert());
        scItemSDO.setSaleUnitName(orderLine.getSaleUnitName());
        //todo 价格怎么能是String呢？应该为Long，金额单位统一为分
        scItemSDO.setUnitPrice(orderLine.getUnitPrice() == null ? null :Long.valueOf(orderLine.getUnitPrice()));
        scItemSDO.setActivityPrice(orderLine.getActivityPrice() == null ? null :Long.valueOf(orderLine.getActivityPrice()));
        scItemSDO.setQuantity(orderLine.getQuantity());
        scItemSDO.setProductationTime(orderLine.getProductationTime());

        orderLineSDO.setScItemSDO(scItemSDO);
        orderLineSDO.setCumulativeReceiptQuantity(orderLine.getCumulativeReceiptQuantity());
        orderLineSDO.setCumulativeProcessingQuantity(orderLine.getCumulativeProcessingQuantity());
        return orderLineSDO;

    }
}
